package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerQuery;
import cn.wolfcode.crm.query.SystemDictionaryQuery;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customer")
public class CustomerPotentialController {

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;


    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/potentialList")
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQuery qo) {
        //设置状态值：只查潜在客户
        qo.setStatus(Customer.STATUS_COMMON);

        //客户信息的可见：管理员和市场经理才可见全部信息，专员只可见自己的
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            //获取当前登录用户的身份信息id
            Employee employee = (Employee) subject.getPrincipal();
            qo.setSellerId(employee.getId());//设置进查询条件
        }

        //根据角色的sn来查询具有某角色的员工 r.sn IN('Market_Manager','Market')
        List<Employee> employees = customerService.selectByRoleSn("Market_Manager", "Market");
        model.addAttribute("employees", employees);

        //职业下拉框
        List<SystemDictionaryItem> jobs = systemDictionaryItemService.selectMsgByDicSn("job");
        model.addAttribute("jobs", jobs);

        //来源下拉框
        List<SystemDictionaryItem> sources = systemDictionaryItemService.selectMsgByDicSn("source");
        model.addAttribute("sources", sources);

        //交流方式下拉框
        List<SystemDictionaryItem> ccts = systemDictionaryItemService.selectMsgByDicSn("communicationMethod");
        model.addAttribute("ccts", ccts);

        //状态下拉框
        //去查状态好像不合理，应该是类中定义的状态
        //Integer[] stauss = customerService.selectByStatus();
        //model.addAttribute("stauss", stauss);


        //调用service的方法的到结果集
        PageInfo pageInfo = customerService.query(qo);
        //共享结果集：在页面遍历显示
        model.addAttribute("result", pageInfo);
        return "customer/potentialList"; //返回视图名称：加上前缀和后缀的ftl
    }

/*

    // 客户列表：老大可见
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo) {

        //客户信息的可见：管理员和市场经理才可见全部信息，专员只可见自己的
        Subject subject = SecurityUtils.getSubject();
        //获取当前登录用户的身份信息id
        Employee employee = (Employee) subject.getPrincipal();
        //可见条件内查询所有客户
        if (employee.isAdmin() || subject.hasRole("Market_Manager")) {
            //根据角色的sn来查询具有某角色的员工 r.sn IN('Market_Manager','Market')
            List<Employee> employees = customerService.selectByRoleSn("Market_Manager", "Market");
            model.addAttribute("employees", employees);

            //调用service的方法得到结果集
            PageInfo pageInfo = customerService.query(qo);
            //共享结果集：在页面遍历显示
            model.addAttribute("result", pageInfo);
        }
        return "customer/list"; //返回视图名称：加上前缀和后缀的ftl
    }
*/


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Customer customer) {
        if (customer.getId() == null) { // 代表新增
            customerService.save(customer);
        } else { // 代表修改
            customerService.update(customer);
        }
        return new JsonResult();
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        //int i = 1/0;
        customerService.delete(id);
        return new JsonResult();
    }


    /**
     * 检查电话号码
     *
     * @return
     */
    @RequestMapping("checkTel")
    @ResponseBody
    public Map<String, Boolean> checkTel(String tel, Long id) {

        Map<String, Boolean> map = new HashMap<>();

        Customer customer = customerService.selectByTel(tel);
        if (id != null) {
            if (tel.equals(customerService.getTelById(id))) {
                //编辑的时候不修改电话，不需要验证
                map.put("valid", true);
            } else {
                map.put("valid", customer == null);//数据库中没有该数据就可以保存
            }
            return map;
        }
        //id为空，表示添加，需要对号码验证
        map.put("valid", customer == null);
        return map;
    }

    /**
     * 检查QQ号码
     *
     * @return
     */
    @RequestMapping("checkQQ")
    @ResponseBody
    public Map<String, Boolean> checkQQ(String qq, Long id) {
        Map<String, Boolean> map = new HashMap<>();
        Customer customer = customerService.selectByQQ(qq);
        //id为空，表示编辑
        if (id != null) {
            //custmer位为空时空指针异常
            if (qq.equals(customerService.getQQById(id))) {
                map.put("valid", true);
            } else {
                map.put("valid", customer == null);//数据库中没有该数据就可以保存
                return map;
            }
        }
        //编辑的时候不需要验证
        map.put("valid", customer == null);
        return map;
    }
}
